package cn.homework.dao.impl;

import cn.homework.dao.FeedbackDao;
import cn.homework.domain.Customers;
import cn.homework.domain.Idea;
import cn.homework.domain.Notice;
import cn.homework.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author ACT
 */
public class FeedbackDaoImpl implements FeedbackDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Idea> findAllFeedback() {
        String sql = "select * from idea";
        List<Idea> list = template.query(sql, new BeanPropertyRowMapper<>(Idea.class));
        return list;
    }

    @Override
    public void addPublishFeedback(String message, String name) {
        String sql = "insert into idea(c_name, c_header, new_message, new_time) values(?, ?, ?, ?)";
        Customers customer = findCustomerByName(name);
        template.update(sql, customer.getName(), customer.getHeader(), message, currentTime());
    }

    @Override
    public void addReplyFeedback(String message, String name, String id) {
        String sql = "update idea set re_name = ? , re_message = ? , re_time = ? where id = ?";
        template.update(sql, name, message, currentTime(), id);
    }

    @Override
    public void delFeedbackById(String id) {
        String sql = "delete from idea where id = ?";
        template.update(sql, id);
    }

    @Override
    public Idea feedbackDetailById(String id) {
        String sql = "select * from idea where id = ?";
        Idea idea = template.queryForObject(sql, new BeanPropertyRowMapper<>(Idea.class), id);
        return idea;
    }

    @Override
    public List<Idea> findFeedbackByCondition(Map<String, String[]> map) {
        String sql = "select * from idea  where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        //遍历map
        Set<String> keySet = map.keySet();
        //定义参数的集合
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            //排除条件信息
            if ("demand".equals(key)) {
                continue;
            }

            //获取values
            String values = map.get(key)[0];
            //判断是否有值
            if (values != null && !"".equals(values)) {
                //有值
                sb.append(" and " + key + " like ? ");
                params.add("%" + values + "%");
            }
        }
        sql = sb.toString();
        List<Idea> list = template.query(sql, new BeanPropertyRowMapper<>(Idea.class), params.toArray());
        return list;
    }

    private Customers findCustomerByName(String name) {
        String sql = "select * from customers where name = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Customers.class), name);
    }

    private String currentTime() {
        Date data = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String string = sdf.format(data);
        return string;
    }
}
